home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP09.ZIP / CHAP09 / PATRON / PAGEWIN.CPP < prev    next >
C/C++ Source or Header  |  1993-06-17  |  23KB  |  840 lines

  1. /*
  2.  * PAGEWIN.CPP
  3.  * Modifications for Chapter 9
  4.  *
  5.  * Window procedure for the Pages window and support functions.  This
  6.  * window manages its own scrollbars and viewport and provides
  7.  * printing capabilities as well.  The public CPages::Print lives here.
  8.  *
  9.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19.  
  20. #include "patron.h"
  21.  
  22.  
  23. extern HWND g_hDlgPrint;
  24. extern BOOL g_fCancelPrint;
  25.  
  26.  
  27. /*
  28.  * PagesWndProc
  29.  *
  30.  * Purpose:
  31.  *  Window procedure for the Pages window.
  32.  */
  33.  
  34. LRESULT __export FAR PASCAL PagesWndProc(HWND hWnd, UINT iMsg
  35.     , WPARAM wParam, LPARAM lParam)
  36.     {
  37.     LPCPages        ppg;
  38.     PAINTSTRUCT     ps;
  39.     HDC             hDC;
  40.     int             iPos, iTmp;
  41.     int             iMin, iMax;
  42.     UINT            idScroll;
  43.     BOOL            fDirty=FALSE;
  44.  
  45.     ppg=(LPCPages)GetWindowLong(hWnd, PAGEWL_STRUCTURE);
  46.  
  47.     switch (iMsg)
  48.         {
  49.         case WM_CREATE:
  50.             ppg=(LPCPages)((LPCREATESTRUCT)lParam)->lpCreateParams;
  51.             SetWindowLong(hWnd, PAGEWL_STRUCTURE, (LONG)ppg);
  52.  
  53.             ppg->m_hWnd=hWnd;
  54.             break;
  55.  
  56.  
  57.         case WM_PAINT:
  58.             /*
  59.              * If there is currently a drag-rectangle showing, then
  60.              * remove it before painting.  This insures that painting
  61.              * doesn't blast part of that rectangle away such that when
  62.              * we draw it next, garbage is left around.
  63.              */
  64.             if (ppg->m_fDragRectShown)
  65.                 ppg->DrawDropTargetRect(NULL, NULL);
  66.  
  67.             hDC=BeginPaint(hWnd, &ps);
  68.  
  69.             //Draw only if we have a page to show.
  70.             if (0!=ppg->m_cPages)
  71.                 ppg->Draw(hDC, FALSE, FALSE);
  72.  
  73.             EndPaint(hWnd, &ps);
  74.  
  75.             //Turn the rectangle back on, if necessary.
  76.             if (ppg->m_fDragRectShown)
  77.                 ppg->DrawDropTargetRect(NULL, NULL);
  78.  
  79.             break;
  80.  
  81.  
  82.         case WM_HSCROLL:
  83.         case WM_VSCROLL:
  84.             idScroll=(WM_HSCROLL==iMsg) ? SB_HORZ : SB_VERT;
  85.  
  86.             iPos=GetScrollPos(hWnd, idScroll);
  87.             iTmp=iPos;
  88.             GetScrollRange(hWnd, idScroll, &iMin, &iMax);
  89.  
  90.             switch (wParam)
  91.                 {
  92.                 case SB_LINEUP:     iPos -= 20;  break;
  93.                 case SB_PAGEUP:     iPos -=100;  break;
  94.                 case SB_LINEDOWN:   iPos += 20;  break;
  95.                 case SB_PAGEDOWN:   iPos +=100;  break;
  96.  
  97.                 case SB_THUMBPOSITION:
  98.                     iPos=ScrollThumbPosition(wParam, lParam);
  99.                     break;
  100.  
  101.                 //We don't want scrolling on this message.
  102.                 case SB_THUMBTRACK:
  103.                     return 0L;
  104.                 }
  105.  
  106.             iPos=max(iMin, min(iPos, iMax));
  107.  
  108.             if (iPos!=iTmp)
  109.                 {
  110.                 //Set the new position and scroll the window as necessary.
  111.                 SetScrollPos(hWnd, idScroll, iPos, TRUE);
  112.  
  113.                 if (SB_HORZ==idScroll)
  114.                     {
  115.                     ppg->m_xPos=iPos;
  116.                     ScrollWindow(hWnd, iTmp-iPos, 0, NULL, NULL);
  117.                     }
  118.                 else
  119.                     {
  120.                     ppg->m_yPos=iPos;
  121.                     ScrollWindow(hWnd, 0, iTmp-iPos, NULL, NULL);
  122.                     }
  123.                 }
  124.  
  125.             break;
  126.  
  127.         //CHAPTER9MOD
  128.         case WM_RBUTTONDOWN:
  129.             if (NULL==ppg->m_pPageCur)
  130.                 break;
  131.  
  132.             fDirty=ppg->m_pPageCur->OnRightDown(wParam
  133.                 , LOWORD(lParam), HIWORD(lParam));
  134.             break;
  135.         //End CHAPTER9MOD
  136.  
  137.         case WM_LBUTTONDOWN:
  138.             if (NULL==ppg->m_pPageCur)
  139.                 break;
  140.  
  141.             fDirty=ppg->m_pPageCur->OnLeftDown(wParam
  142.                 , LOWORD(lParam), HIWORD(lParam));
  143.             break;
  144.  
  145.         case WM_LBUTTONUP:
  146.             if (NULL==ppg->m_pPageCur)
  147.                 break;
  148.  
  149.             fDirty=ppg->m_pPageCur->OnLeftUp(wParam
  150.                 , LOWORD(lParam), HIWORD(lParam));
  151.             break;
  152.  
  153.         case WM_LBUTTONDBLCLK:
  154.             if (NULL==ppg->m_pPageCur)
  155.                 break;
  156.  
  157.             fDirty=ppg->m_pPageCur->OnLeftDoubleClick(wParam, LOWORD(lParam)
  158.                 , HIWORD(lParam));
  159.             break;
  160.  
  161.         case WM_MOUSEMOVE:
  162.             if (NULL==ppg->m_pPageCur)
  163.                 break;
  164.  
  165.             ppg->m_pPageCur->OnMouseMove(wParam, LOWORD(lParam)
  166.                 , HIWORD(lParam));
  167.             break;
  168.  
  169.         case WM_NCHITTEST:
  170.             if (NULL!=ppg->m_pPageCur)
  171.                 {
  172.                 //This just saves information in the page for OnSetCursor
  173.                 ppg->m_pPageCur->OnNCHitTest(LOWORD(lParam)
  174.                     , HIWORD(lParam));
  175.                 }
  176.  
  177.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  178.  
  179.         case WM_SETCURSOR:
  180.             if (NULL!=ppg->m_pPageCur)
  181.                 {
  182.                 if (ppg->m_pPageCur->OnSetCursor(LOWORD(lParam)))
  183.                     break;
  184.                 }
  185.  
  186.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  187.  
  188.         default:
  189.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  190.         }
  191.  
  192.     ppg->m_fDirty |= fDirty;
  193.     return 0L;
  194.     }
  195.  
  196.  
  197.  
  198.  
  199.  
  200. /*
  201.  * RectConvertMappings
  202.  *
  203.  * Purpose:
  204.  *  Converts the contents of a rectangle from device to logical
  205.  *  coordinates where the hDC defines the logical coordinates.
  206.  *
  207.  * Parameters:
  208.  *  pRect           LPRECT containing the rectangle to convert.
  209.  *  hDC             HDC describing the logical coordinate system.
  210.  *                  if NULL, uses a screen DC in MM_LOMETRIC.
  211.  *  fToDevice       BOOL TRUE to convert from uConv to device,
  212.  *                  FALSE to convert device to uConv.
  213.  *
  214.  * Return Value:
  215.  *  None
  216.  */
  217.  
  218. void RectConvertMappings(LPRECT pRect, HDC hDC, BOOL fToDevice)
  219.     {
  220.     POINT   rgpt[2];
  221.     BOOL    fSysDC=FALSE;
  222.  
  223.     if (NULL==pRect)
  224.         return;
  225.  
  226.     rgpt[0].x=pRect->left;
  227.     rgpt[0].y=pRect->top;
  228.     rgpt[1].x=pRect->right;
  229.     rgpt[1].y=pRect->bottom;
  230.  
  231.     if (NULL==hDC)
  232.         {
  233.         hDC=GetDC(NULL);
  234.         SetMapMode(hDC, MM_LOMETRIC);
  235.         fSysDC=TRUE;
  236.         }
  237.  
  238.     if (fToDevice)
  239.         LPtoDP(hDC, rgpt, 2);
  240.     else
  241.         DPtoLP(hDC, rgpt, 2);
  242.  
  243.     if (fSysDC)
  244.         ReleaseDC(NULL, hDC);
  245.  
  246.     pRect->left=rgpt[0].x;
  247.     pRect->top=rgpt[0].y;
  248.     pRect->right=rgpt[1].x;
  249.     pRect->bottom=rgpt[1].y;
  250.  
  251.     return;
  252.     }
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. /*
  261.  * CPages::Draw
  262.  *
  263.  * Purpose:
  264.  *  Paints the current page in the pages window.
  265.  *
  266.  * Parameters:
  267.  *  hDC             HDC to draw on, could be a metafile or printer DC or
  268.  *                  any other type of DC.
  269.  *  fNoColor        BOOL indicating if we should use screen colors or
  270.  *                  printer colos (B&W).  Objects are printed as-is, however.
  271.  *                  This is TRUE for printer DCs or print preview.
  272.  *  fPrinter        BOOL indicating if this is a printer DC in which case
  273.  *                  we eliminate some of the fancy drawing, like shadows on
  274.  *                  the page and so forth.
  275.  *
  276.  * Return Value:
  277.  *  None
  278.  */
  279.  
  280. void CPages::Draw(HDC hDC, BOOL fNoColor, BOOL fPrinter)
  281.     {
  282.     RECT            rc, rcT;
  283.     UINT            uMM;
  284.     HPEN            hPen;
  285.     HBRUSH          hBrush;
  286.     HGDIOBJ         hObj1, hObj2;
  287.     COLORREF        cr;
  288.     char            szTemp[20];
  289.     UINT            cch;
  290.     DWORD           dwExt;
  291.     LPPAGE          pPage;
  292.     RECT            rcPos;
  293.  
  294.     //Make sure the DC is in LOMETRIC
  295.     uMM=SetMapMode(hDC, MM_LOMETRIC);
  296.  
  297.     if (!fPrinter)
  298.         {
  299.         /*
  300.          * We maintain a 6mm border around the page on the screen besides
  301.          * 12.7mm margins.  We also have to account for the scroll position
  302.          * with m_*Pos which are in pixels so we have to convert the